package com.util.file;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 썸네일 이미지 생성하기
 * 
 * @author
 */
public class Th_ThumbnailGen implements Runnable {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	private String filePath;
	private String fileName;
	private List<Integer> thumbnailSizeList;

	public Th_ThumbnailGen(String filePath, String fileName, List<Integer> thumbnailSizeList) {
		this.filePath = filePath;
		this.fileName = fileName;
		this.thumbnailSizeList = thumbnailSizeList;
	}

	public void run() {
		try {
			Thread.sleep(100);

			File fid = new File(filePath + fileName);
			if (!fid.exists()) {
				logger.warn("File <{}> is not existed", filePath + fileName);
				return;
			}

			EasyImage easyImage = new EasyImage(fid);
			if (!easyImage.isSuppoprtedImageFormat()) {
				logger.warn("File <{}> is not suppoprted format", filePath + fileName);
				return;
			}

			/* resize */
			int size = 0;
			for (int iCnt = 0; iCnt < thumbnailSizeList.size(); iCnt++) {
				size = thumbnailSizeList.get(iCnt);
				String newFilePath = filePath + size + File.separator;

				EasyImage resizedImage = easyImage.resize(size, size);
				File dir = new File(newFilePath);
				if (!dir.exists())
					dir.mkdir();

				File fid2 = new File(newFilePath + fileName);
				FileOutputStream out = new FileOutputStream(fid2);
				resizedImage.writeTo(out, "jpg");

				logger.info("Result: {} file is generated", newFilePath + fileName);
				Thread.sleep(200);
			}
		} catch (Exception e) {
			logger.error("Error: {}", e.getMessage());
		}
	}

}
